﻿Ext.define('Html5App.controller.MainController', {
	extend : 'Ext.app.Controller',
	config : {
		models : ['Config'],
		views : ['Main', 'Html5App.view.Login'],
		refs : {
			main : 'main',
			redirectBtn : 'button[action=redirect]',
			redirectByParamsBtn : 'button[action=redirectByParams]'
		},
		control : {
			// 跳转按钮
			redirectBtn : {
				tap : function(t, value) {
					this.redirectTo('redirec/' + t.config.redirect);
				}
			},
			// 动态传参
			redirectByParamsBtn : {
				tap : function(t, value) {
					config.tmpParams = t.config.params;
					this.redirectTo('redirec/' + t.config.redirect);
				}
			}
		},
		// 路由，由redirectTo方法触发
		routes : {
			'main' : 'showMian',
			'redirec/:view' : 'redirec',
			'redirec/:view/:isPop' : 'redirec',
			'firstStart' : 'showFirstStart'
		}
	},
	launch : function() {
		var me = this;
		config.redirec = me;
		document.addEventListener("deviceready", onDeviceReady, false);
		function onDeviceReady() {
			document.addEventListener("backbutton", backTap, false);
			function backTap() {
				me.onbackTap();
			}
		}
		this.redirectTo('main');
	},
	// 显示首页
	showMian : function() {
		util.ePush('main');
	},
	// 动态传参
	// 显示视图xtype:这里是指alternateClassName
	redirec : function(xtype, isPop) {
		var params = config.tmpParams;
		this.pushView({
					xtype : xtype,
					params : params,
					isPop : isPop
				});
	},
	pushView : function(params) {
		var main = this.getMain();
		if (!main)
			return;
		this.isExit = false;
		if (params.isPop) {
			var xtype = false;;
			if (params.xtype != 0) {
				xtype = params.xtype;
			}
			main.popAndPush(xtype, params.params);
		} else {
			main.push(params.xtype, params.params);
		}
		delete config.tmpParams;
	},
	// 监听Android返回键
	onbackTap : function() {
		var cardPanel = this.getMain();
		if (cardPanel) {
			var back = cardPanel.viewStack;
			if (back.length > 1) {
				this.appExit();
				//cardPanel.onBackButtonTap();
			} else {
				this.appExit();
			}
		} else {
			this.appExit();
		}
	},
	appExit : function() {
		if (this.isExit) {
			navigator.notification.confirm(
				'確定要退出嗎？',
				function(btnId){
					if(btnId==2){
						navigator.app.exitApp();
					}
		        }, '溫馨提示', ['取消','確定']
			);
		} else {
			this.isExit = true;
			setTimeout(
				function() {
					this.isExit = false;
				}, 
				2000
			);
			//util.showMessageMask('再按一次退出','info-max');
		}
	}
});